home *** CD-ROM | disk | FTP | other *** search
- #undef DEBUG
- #undef CHECK_MAGIC
-
- /*
- * whatis - show description of word from man database
- *
- * whatis [-l] [-P path] [-s sect] name ...
- *
- * bugs:
- * - need more error checking (of data in database)
- * - perhaps a "magic number" for the database? it is
- * written, but #ifdef CHECK_MAGIC.
- *
- * todo:
- * - allow "-s n name name -s m name ..."
- * - allow continuation lines in database:
- *
- * name\
- * %1\
- * %...
- */
-
- static char *rcsid_whatis_c = "$Id: whatis.c,v 2.0 1992/09/13 05:02:44 rosenkra Exp $";
- /*
- * $Log: whatis.c,v $
- * Revision 2.0 1992/09/13 05:02:44 rosenkra
- * total rewrite. this if first rev of this file.
- *
- *
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
-
-
- #include "whatis.h"
-
-
- /*
- * we only need one record here. we read a record and check all the
- * command line args against it for a match
- */
- struct rec r;
-
- char *libpath = MANPATH; /* path to database files (-P) */
- int debugging = 0; /* for -d */
-
-
- /*
- * fcn prototypes
- */
- void whatis (int, int, int, char **);
- void usage (int);
- int parse_record (char *, struct rec *);
- void print_record (int, struct rec *);
- #ifdef CHECK_MAGIC
- int check_magic (void);
- #endif
-
-
-
-
- /*------------------------------*/
- /* main */
- /*------------------------------*/
- void main (int argc, char *argv[])
- {
- int sect = -1;
- int verbose = 0;
- char *lpath;
- char *ps;
-
-
-
- /*
- * see if there is MANPATH in env. use it over default...
- */
- if ((lpath = getenv ("MANPATH")) != (char *) NULL)
- libpath = lpath;
- else if ((lpath = getenv ("MANDIR")) != (char *) NULL)
- libpath = lpath;
- #ifdef DEBUG
- else
- fprintf (stderr, "whatis: environment variable MANPATH not set, using default\n");
- #endif
-
-
- /*
- * parse args
- */
- for (argc--, argv++; argc && **argv == '-'; argc--, argv++)
- {
- switch (*(*argv+1))
- {
- case 'P': /* path for db */
- case 'M':
- argc--, argv++;
- if (argc < 1)
- usage (1);
- libpath = *argv;
- break;
-
- case 's': /* specific section */
- argc--, argv++;
- if (argc < 1)
- usage (1);
- sect = **argv;
- break;
-
- case 'l': /* long (verbose) */
- verbose = 1;
- break;
-
- case 'd': /* debug mode */
- debugging = 1;
- break;
-
- case 'h': /* help */
- usage (0);
- /* NOTREACHED */
- }
- }
-
-
-
- /*
- * whatis what?
- */
- if (argc == 0)
- {
- fprintf (stderr,
- "whatis: you must specify an argument. consider using whatisin.\n");
- usage (1);
- }
-
-
-
- /*
- * not documented: if first arg is a number, use it for section...
- */
- if (isdigit(**argv))
- {
- sect = **argv;
- argc--, argv++;
- }
-
-
-
- /*
- * do it. if specific section, search it only. else search all
- * sections
- */
- if (sect != -1)
- {
- whatis (verbose, sect, argc, argv);
- }
- else
- {
- for (ps = SECTIONS; *ps; ps++)
- {
- sect = *ps;
- whatis (verbose, sect, argc, argv);
- }
- }
-
-
- exit (0);
- }
-
-
-
-
- /*------------------------------*/
- /* whatis */
- /*------------------------------*/
- void whatis (int verbose, int sect, int argc, char **argv)
- {
-
- #define MAX_ARGS 100
-
- char dbname[256];
- char buf[REC_SIZE];
- register char **vp;
- int notfound[MAX_ARGS];
- int i;
-
-
-
- /*
- * FIXME: we should allocate space for the notfound list.
- */
- if (argc > MAX_ARGS)
- argc = MAX_ARGS;
-
-
-
- /*
- * set up db name. section is really a single ascii char, not an
- * int. this is so we can have whatis for local, new, etc.
- */
- if (sect == -1)
- /* orig behavior (just single "whatis" file, never used here) */
- sprintf (dbname, "%s%s%s", libpath, SLASH, WHATIS);
- else
- /* new: whatis._[0-9lno]_ */
- sprintf (dbname, "%s%s%s._%c_", libpath, SLASH, WHATIS, sect);
-
-
-
- /*
- * reopen stdin as this file...
- */
- if (debugging)
- fprintf (stderr, "checking database file %s...\n", dbname);
- if (freopen (dbname, "r", stdin) == (FILE *) NULL)
- {
- /* if (verbose || debugging)*/
- if (debugging)
- fprintf (stderr,
- "whatis: could not access file %s\n", dbname);
-
- return;
- }
-
-
-
- #ifdef CHECK_MAGIC
- /*
- * check file's magic
- */
- if (check_magic ())
- {
- fprintf (stderr,
- "whatis: magic number is wrong for file %s\n",
- dbname);
-
- return;
- }
- #endif
-
-
-
- /*
- * read file and compare. first assume we will not find anything
- * and flag list...
- */
- for (i = 0; i < MAX_ARGS; i++)
- notfound[i] = 1;
- while (1)
- {
- /*
- * get raw record from file
- */
- fgets (buf, REC_SIZE-1, stdin);
- if (feof (stdin))
- break;
- if (debugging)
- fprintf (stderr, "%s", buf);
-
-
-
- /*
- * skip comment or blank lines
- */
- if (buf[0] == '#' || buf[0] == '\0' || buf[0] == '\n')
- continue;
-
-
-
- /*
- * parse the record and store in r
- */
- parse_record (buf, &r);
-
- if (debugging)
- {
- fprintf (stderr, "name: %s\n", r.name ? r.name : "(NULL)");
- fprintf (stderr, "section: %s\n", r.section ? r.section : "(NULL)");
- fprintf (stderr, "subsect: %s\n", r.subsect ? r.subsect : "(NULL)");
- fprintf (stderr, "desc: %s\n", r.desc ? r.desc : "(NULL)");
- }
-
-
-
- /*
- * compare record's name field to all args from orig
- * command line. if we find one, flag notfound list
- */
- for (i = 0, vp = argv; i < argc && *vp; vp++, i++)
- {
- if (!strncmp (r.name, *vp, strlen (*vp)))
- {
- print_record (verbose, &r);
- notfound[i] = 0;
- }
- }
- }
-
-
-
- /*
- * print a message if we didn't find anything for the cmdline arg
- */
- if (verbose)
- {
- for (i = 0; i < argc; i++)
- {
- if (notfound[i])
- {
- printf ("nothing found in section %c for %s\n",
- sect, argv[i]);
- }
- }
- }
-
- return;
- }
-
-
-
-
- /*------------------------------*/
- /* usage */
- /*------------------------------*/
- void usage (int excode)
- {
- #define U(x) fprintf(stderr,x);
-
- U("usage: whatis [-l] [-P path] [-s section] name ...\n");
- U(" -l long list\n");
- U(" -P path alternative path to databases (MANPATH)\n");
- U(" -s section search single section, not all\n");
- U(" name valid name of a command, etc. if name is unknown,\n");
- U(" consider using whatisin(1) or apropos(1).\n");
-
- exit (excode);
- }
-
-